package a07_二叉树;

/**
 * <p>
 * a13_路径总和
 * </p>
 * 遍历顺序：都可以，因为不用处理中节点
 *
 * @author flyduck
 * @since 2024-09-02
 */
public class a17_路径总和复习1 {

    public static void main(String[] args) {
//        TreeNode treeNode1 = new TreeNode(11, new TreeNode(7), new TreeNode(2));
//        TreeNode treeNode2 = new TreeNode(4, null, new TreeNode(1));
//        TreeNode left = new TreeNode(4, treeNode1, null);
//        TreeNode right = new TreeNode(8, new TreeNode(13), treeNode2);
//        TreeNode treeNode = new TreeNode(5, left, right);
        TreeNode treeNode = new TreeNode(1, new TreeNode(1), new TreeNode(1));
        a17_路径总和复习1 a17_路径总和 = new a17_路径总和复习1();
        boolean b = a17_路径总和.hasPathSum(treeNode, 3);
        System.out.println("b = " + b);
    }


    public boolean hasPathSum(TreeNode root, int targetSum) {
        if (root == null) {
            return false;
        }
        if (root.left == null && root.right == null) {
            return root.val == targetSum;
        }
        boolean leftFlag = hasPathSum(root.left, targetSum - root.val);
        if (leftFlag) {
            return true;
        }
        boolean rightFlag = hasPathSum(root.right, targetSum - root.val);
        if (rightFlag) {
            return true;
        }
        return false;
    }


}
